from forward.api.admin.schemas.setting import SettingLoginInSchema
from forward.utils.config import ConfigUtil


class SettingLoginService:
    """登录设置服务实现类"""

    async def detail(self) -> dict:
        """登录设置详情"""
        config = await ConfigUtil.get("login")
        return {
            "loginWay": [
                int(i) for i in config.get("loginWay", "").split(",") if i.isdigit()
            ],  # 登录方式
            "forceBindMobile": config.get("forceBindMobile", 0),  # 强制绑定手机
            "openAgreement": config.get("openAgreement", 0),  # 是否开启协议
            "openOtherAuth": config.get("openOtherAuth", 0),  # 第三方的登录
            "autoLoginAuth": [
                int(i)
                for i in config.get("autoLoginAuth", "").split(",")
                if i.isdigit()
            ],  # 自动登录授权
            "wxOauthScope": config.get("wxOauthScope", 0),  # 微信公众号授权登录的方式
        }

    async def save(self, login_in: SettingLoginInSchema):
        """登录设置保存"""
        await ConfigUtil.set("login", "loginWay", login_in.login_way)
        await ConfigUtil.set(
            "login", "forceBindMobile", str(login_in.force_bind_mobile)
        )
        await ConfigUtil.set("login", "openAgreement", str(login_in.open_agreement))
        await ConfigUtil.set("login", "openOtherAuth", str(login_in.open_other_auth))
        await ConfigUtil.set("login", "autoLoginAuth", login_in.auto_login_auth)
        await ConfigUtil.set("login", "wxOauthScope", login_in.wx_oauth_scope)

    @classmethod
    async def instance(cls):
        """实例化"""
        return cls()
